<?php

/**
 * @category   mediad2
 * @package    Mediad2_Service
 */
class Mediad2_Service_Admin_Dict extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('Mediad2_Mapper_Dict');
		$this->setModel('Mediad2_Model_Dict');
	}
	
	/**
	 * 返回指定字典类型的字典信息记录的总数
	 *
	 * @param string $dtid
	 * @return int The number of rows
	 */
	public function getCountDictInfoByDictType($dtid = null)
	{
		$mapper = $this->getMapper();
		$condition = null;
		if (!null == $dtid) {
			$selector = $mapper->getDao()->select();
			$adapter = $mapper->getDao()->getAdapter();
			$parms = array( 'dtid'=>$dtid, );
			foreach ($parms as $key=>$value) {
				$selector->where($adapter->quoteInto("$key=?",$value));
			}
			$condition = implode(' ', $selector->getPart('where'));
		}
		$result = $mapper->getCount($condition);
		return $result;
	}
	
	
	/**
	 * 根据字典类型ID，返回字典的其他信息
	 *
	 * @param string $dtid
	 * @param integer $pageIndex
	 * @param integer $pageNum
	 * @param string $where
	 * @param string $order
	 * @param boolean $returnArray
	 * @return array Mediad_Model_GloDict
	 */
	public function fetchAllDictInfoByType($dtid = null, $pageIndex = 1, $pageNum = 20, $where = null, $order = null, $returnArray = false)
	{
		$mapper = $this->getMapper();
	
		if (!null == $dtid) {
			$selector = $mapper->getDao()->select();
			$adapter = $mapper->getDao()->getAdapter();
			$parms = array( 'dtid'=>$dtid, );
			foreach ($parms as $key=>$value) {
				$selector->where($adapter->quoteInto("$key=?",$value));
			}
			$condition = implode(' ', $selector->getPart('where'));
			if (!null == $where) {
				$where = $where . ' AND ' . $condition;
			} else {
				$where = $condition;
			}
		}
	
		$results = $mapper->fetchAll($pageIndex, $pageNum, $where, $order);
		$models = array();
		foreach ($results as $key=>$value) {
			$model = new $this->modelClassName($value);
			if ($returnArray == true) {
				$models[] = $model->toArray();
			} else {
				$models[] = $model;
			}
		}
		return $models;
	}
	
	/**
	 * 根据字典类型Code，返回字典的其他信息
	 *
	 * @param string $dtcode
	 * @param integer $pageIndex
	 * @param integer $pageNum
	 * @param string $where
	 * @param string $order
	 * @param boolean $returnArray
	 * @return array Mediad_Model_Dict
	 */
	public function fetchAllDictInfoByTypeCode($dtcode = null, $pageIndex = 1, $pageNum = 20, $where = null, $order = null, $returnArray = false)
	{
		$models = array();
	
		//通过字典类型代码，获取对应的字典类型ID
		$serviceDictType = new Mediad2_Service_Admin_DictType();
		$modelDictType = $serviceDictType->findDictTypeByCode($dtcode);
		$dtid = $modelDictType->getDtid();
	
		//判断DictTypeCode是否合法，如果不合法，则DictTypeId为null，那么直接返回一个空的数组
		if (empty($dtid)) {
			return $models;
		}
	
		$order[] = 'dictid desc';	//增加排序
	
		//调用fetchAllDictInfoByType，返回字典的其他信息
		$models = $this->fetchAllDictInfoByType($dtid, $pageIndex, $pageNum, $where, $order, $returnArray);
		return $models;
	}
	
	/**
	 * 批量删除指定的字典记录
	 *
	 * @param array of $dictid
	 * @return boolean return true if no error.
	 */
	public function deleteBatch(array $arrayDictId)
	{
		if ( count($arrayDictId) == 0 ) {
			return false;
		}
	
		//构造where条件
		$where = 'dictid in ( ' . implode(",", $arrayDictId) . ')';
	
		$mapper = $this->getMapper();
		$dao = $mapper->getDao();
		$result = $dao->delete($where);
		if ($result > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 根据字典类型Code，获取字典数组
	 *
	 * @param string $dtcode
	 * @param string $where
	 * @return array the data include key/value/output
	 */
	public function getDictsByDictTypeCode($dtcode, $where = null)
	{
		$result = array();
	
		$models = $this->fetchAllDictInfoByTypeCode($dtcode,null,null,$where);
	
		foreach ($models as $key => $model) {
			$result['name'][]   = $model->getName();
			$result['code'][] 	= $model->getCode();
			$result['value'][] 	= $model->getValue();
			$result['output'][] = $model->getDesc();
			$result['status'][] = $model->getStatus();
			$result['num'][]	= $model->getNum();
			$result['data'][$model->getCode()] = $model->getValue();
		}
	
		return $result;
	}
	
}